package com.slipper.system.ai.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.ai.dao.AiSupplierDao;
import com.slipper.system.ai.bo.AiSupplierBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class AiSupplierService {
    @Resource
    private AiSupplierDao aiSupplierDao;
    
    public AiSupplierBO queryBy(String id) {
        return aiSupplierDao.queryBy(id);
    }
    
    public List<AiSupplierBO> queryList(AiSupplierBO bo) {
        return aiSupplierDao.queryList(bo);
    }
    
    public PageInfo<AiSupplierBO> queryPage(AiSupplierBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(aiSupplierDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(AiSupplierBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return aiSupplierDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return aiSupplierDao.insert(bo);
        }
    }

    @WriteTransactional
    public void saveList(List<AiSupplierBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (aiSupplierDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                aiSupplierDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                aiSupplierDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(AiSupplierBO bo) {
        return aiSupplierDao.delete(bo);
    }
}
